package com.telecom.cms.dao;

import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import com.telecom.cms.dto.CimPage;
import com.telecom.cms.util.Constant;
import com.telecom.cms.util.CurrentTime;

public class CimPageDAO {
	Dao dao = new DaoImpStd();

	private static List cimpagelist=null;
	String db = (new Constant()).getPara("DATABASE");

	public int insert(CimPage transientInstance) {
		int rs = 0;
		String sql = "select * from  " + db + ".Cim_Page where page_id="
				+ transientInstance.getPageId();
		try {
			ResultSet rs2 = dao.select(sql);
			if (rs2.next())
				sql = "update " + db + ".Cim_Page set latn_Id='"
			        	+ transientInstance.getLatnId() + "',page_Name='"
						+ transientInstance.getPageName() + "',page_Desc='"
						+ transientInstance.getPageDesc() + "',show_Type='"
						+ transientInstance.getShowType() + "',cluster_Type='"
						+ transientInstance.getClusterType() + "',Creator_Name='"
						+ transientInstance.getCreatorName()
						+ "',Create_Time=timestamp '"
						+ CurrentTime.getCurrentTime() + "'where page_id="
						+ transientInstance.getPageId();
			else
				sql = "insert into " + db + ".Cim_Page values(,'"
						+ transientInstance.getLatnId() + "' , '"
						+ transientInstance.getPageName() + "' , '"
						+ transientInstance.getPageDesc() + "' , '"
						+ transientInstance.getShowType() + "' , '"
						+ transientInstance.getClusterType() + "' ,'"
						+ transientInstance.getCreatorName() + "' ,"
						+ " timestamp '"
						+ CurrentTime.getCurrentTime() + "')";
			rs = dao.insert(sql);
			if (rs2 != null) {

				rs2.close();
			}

		} catch (Exception re) {
			System.out.println("save failed:" + re);
		}
		finally
		{
			dao.closeconn();
		}
		return rs;

	}

	public void delete(CimPage persistentInstance) {

		String sql = "delete from " + db + ".Cim_Page where Page_Id = "
				+ persistentInstance.getPageId();

		try {
			dao.delete(sql);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	public CimPage findById(String id) {

		if(cimpagelist==null)
			findAll();
		for(int i=0;i<cimpagelist.size();i++)
			if(((CimPage)cimpagelist.get(i)).getPageId()==Integer.parseInt(id))
				return (CimPage)cimpagelist.get(i);
		return null;
		/*String sql = "select * from " + db + ".Cim_Page where Page_Id = "+ id;
		
		try {
			ResultSet rs = dao.select(sql);
			if (rs.next()) {
				sd.setPageId(rs.getInt(1));
				sd.setLatnId(rs.getString(2));
				sd.setPageName(rs.getString(3));
				sd.setPageDesc(rs.getString(4));
				sd.setShowType(rs.getString(5));
				sd.setClusterType(rs.getString(6));
				sd.setCreatorName(rs.getString(7));
				sd.setCreateTime(rs.getString(8));

			}
			if (rs != null) {

				rs.close();
			}

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		finally
		{
			dao.closeconn();
		}*/
		//return sd;

	}

	public List findAll() {

		if(cimpagelist!=null)
			return cimpagelist;
		return findAllForce();

	}
	
	public List findAllForce() {

		String sql = "select * from " + db + ".Cim_Page order by page_id";

		List list = new ArrayList();
		try {
			ResultSet rs = dao.select(sql);
			while (rs.next()) {
				CimPage sd = new CimPage();

				sd.setPageId(rs.getInt(1));
				sd.setLatnId(rs.getString(2));
				sd.setPageName(rs.getString(3));
				sd.setPageDesc(rs.getString(4));
				sd.setShowType(rs.getString(5));
				sd.setClusterType(rs.getString(6));
				sd.setCreatorName(rs.getString(7));
				sd.setCreateTime(rs.getString(8));
				
				list.add(sd);
			}
			if (rs != null) {

				rs.close();
			}

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		finally
		{
			dao.closeconn();
		}
		cimpagelist=list;
		return list;

	}
	
	public List findAll(String clustertype) {
		List list = new ArrayList();
		if(cimpagelist==null)
			findAll();
		CimPage sd = new CimPage();
		for(int i=0;i<cimpagelist.size();i++)
			if(((CimPage)cimpagelist.get(i)).getClusterType().equals(clustertype))
				list.add((CimPage)cimpagelist.get(i));
		return list;
	}
	
	public List findAll(String latnid,String clustertype) {
		List list = new ArrayList();
		if(cimpagelist==null)
			findAll();
		CimPage sd = new CimPage();
		for(int i=0;i<cimpagelist.size();i++)
			if(((CimPage)cimpagelist.get(i)).getClusterType().equals(clustertype)&&((CimPage)cimpagelist.get(i)).getLatnId().equals(latnid))
				list.add((CimPage)cimpagelist.get(i));
		return list;

	}

}